package com.ls.io.rpc.transport;

import com.ls.io.rpc.protobuf.RpcContent;
import com.ls.io.rpc.protobuf.RpcHeader;
import com.ls.io.rpc.protobuf.RpcPackage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.CompletableFuture;

public class ResponseChannelHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        RpcPackage pck = (RpcPackage)msg;
        RpcContent content = pck.getContent();
        RpcHeader header = pck.getHeader();

//        System.out.println("client receive: " + pck.getHeader().getRequestId());

        CompletableFuture response = ResponseContext.getResponse(header.getRequestId());

        if(response == null){
            System.err.println("resp requestId:" + header.getRequestId() + ": result" + content.getResult());
        }else{
            response.complete(content.getResult());
        }

    }

}
